package com.xn.app.muying.fragment;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.xn.app.muying.activity.DengLuActivity;
import com.xn.app.muying.activity.GouMaiActivity;
import com.xn.app.muying.activity.MainActivity;
import com.xn.app.muying.activity.MuYingApplication;
import com.xn.app.muying.R;
import com.xn.app.muying.activity.ShouHouActivity;
import com.xn.app.muying.activity.WebActivity;
import com.xn.app.muying.activity.YanZhengActivity;
import com.xn.app.muying.activity.ZhangHuActivity;
import com.xn.app.muying.activity.FeedBackActivity;
import com.xn.app.muying.activity.HealthRecordsActivity;
import com.xn.app.muying.activity.JiuZhenActivity;
import com.xn.app.muying.activity.MineOrderActivity;
import com.xn.app.muying.activity.MyOrderActivity;
import com.xn.app.muying.activity.PersonalActivity;
import com.xn.app.muying.activity.VIPActivity;
import com.xn.app.muying.activity.YunQiXuanZeActivity;
import com.xn.app.muying.adpter.MyFunctionAdapter;
import com.xn.app.muying.sqlite.DBManager;
import com.xn.app.muying.util.APIMethod;
import com.xn.app.muying.util.AppConstant;
import com.xn.app.muying.util.Constants;
import com.xn.app.muying.util.DateUtils;
import com.xn.app.muying.util.MyAlertDialog;
import com.xn.app.muying.util.NetworkUtil;
import com.xn.app.muying.util.ToastUtil;
import com.xn.app.muying.view.MyGridView;
import com.xn.app.muying.view.RoundImageView;
import com.zhuge.analysis.stat.ZhugeSDK;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * 我的---页面
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends Fragment {
    //头像
    private RoundImageView image = null;
    //设置
    private ImageView shezhi = null;
    //个人信息 孕期显示
    private TextView yunqi_textview = null;
    //个人信息 会员显示
    private ImageView vip_imageview = null;
    //个人信息 会员到期时间显示
    private TextView vip_endtime = null;
    //个人信息 续费按钮显示
    private Button xufei = null;
    //个人信息布局
    private LinearLayout user_linearlayout = null;
    private LinearLayout vip_linearlayout = null;
    //登陆按钮
    private TextView deng_lu = null;
    //注册按钮
    private TextView zhu_ce = null;
    //登录注册布局
    private LinearLayout no_user = null;
    //显示用户账号
    private TextView yong_hu = null;
    //获取登陆状态
    private boolean isLogin = false;
    //获取用户手机号码参数
    private String yh = null;
    public final static int FORRESULT = 2;
    //记录当前页面是否打开过
    private boolean boo = false;
    //数据库
    private DBManager db = null;
    //获取用户姓名
    private String userName = null;
    //用来保存头像的集合
    private ArrayList<HashMap<String, String>> list = null;
    //一天的时间戳  86400为一天的时间戳
    private final long oneDay = AppConstant.oneDay;
    //个人功能模块
    private MyGridView my_gridview = null;
    //用来保存功能模块的集合
    private ArrayList<HashMap<String, String>> my_list = null;
    //其他功能模块
    private MyGridView other_gridview = null;
    //用来保存功能模块的集合
    private ArrayList<HashMap<String, String>> other_list = null;

    /**
     * 用来接收更新数据
     *
     * @param i
     */
    public void MineFragment(int i) {
        //如果页面没有打开过，说明控件也没有初始化过，不打开执行该操作
        if (boo) {
            //如果本地用户数据不为空，就显示用户信息
            userName = db.select("tel");
            //i=0  获取数据    i=1 取消数据
            if (i == 0) {
                if (userName != null && !userName.equals("") && !userName.equals("null")) {
                    yong_hu.setVisibility(View.VISIBLE);
                    no_user.setVisibility(View.GONE);
                    yong_hu.setText(userName.replaceAll("(\\d{3})\\d{5}(\\d{3})", "$1*****$2"));
                    getData();
                } else {
                    //为空就提示登陆
                    startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                }
            } else {
                //退出后显示
                yong_hu.setVisibility(View.GONE);
                no_user.setVisibility(View.VISIBLE);
                user_linearlayout.setVisibility(LinearLayout.GONE);
                vip_linearlayout.setVisibility(LinearLayout.GONE);
                image.setImageResource(R.mipmap.moren);
            }
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ScrollView wode = (ScrollView) inflater.inflate(R.layout.fragment_wo_de, container, false);
        initView(wode);
        return wode;
    }

    /**
     * 初始化控件及功能实现
     */
    private void initView(ScrollView wode) {
        boo = true;
        db = new DBManager(getContext());
        image = (RoundImageView) wode.findViewById(R.id.image);
        deng_lu = (TextView) wode.findViewById(R.id.deng_lu);
        zhu_ce = (TextView) wode.findViewById(R.id.zhu_ce);
        no_user = (LinearLayout) wode.findViewById(R.id.no_user);
        yunqi_textview = (TextView) wode.findViewById(R.id.yunqi_textview);
        vip_imageview = (ImageView) wode.findViewById(R.id.vip_imageview);
        shezhi = (ImageView) wode.findViewById(R.id.shezhi);
        vip_endtime = (TextView) wode.findViewById(R.id.vip_endtime);
        xufei = (Button) wode.findViewById(R.id.xufei);
        user_linearlayout = (LinearLayout) wode.findViewById(R.id.user_linearlayout);
        vip_linearlayout = (LinearLayout) wode.findViewById(R.id.vip_linearlayout);
        yong_hu = (TextView) wode.findViewById(R.id.yong_hu);
        my_gridview = (MyGridView) wode.findViewById(R.id.my_gridview);
        other_gridview = (MyGridView) wode.findViewById(R.id.other_gridview);
        //填充功能组件
        setFunction();
        //如果本地用户数据不为空，就显示用户信息
        userName = db.select("tel");
        //设置头像数据
        setHead();
        if (userName != null && !userName.equals("") && !userName.equals("null")) {
            //显示缓存数据
            cache();
            //下载数据
            if (NetworkUtil.isNetworkAvailable(getActivity())) {
                getData();
            }
        } else {
            //为空就提示登陆
            image.setImageResource(R.mipmap.moren);
            yong_hu.setVisibility(View.GONE);
            no_user.setVisibility(View.VISIBLE);
        }
        //添加点击事件
        image.setOnClickListener(onclicklistener);
        deng_lu.setOnClickListener(onclicklistener);
        zhu_ce.setOnClickListener(onclicklistener);
        xufei.setOnClickListener(onclicklistener);
        shezhi.setOnClickListener(onclicklistener);
        //添加功能选择功能
        my_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (my_list.size() > 0 && position < 6) {
                    if (userName != null && !userName.equals("") && !userName.equals("null")) {
                        if (NetworkUtil.isNetworkAvailable(MainActivity.mActivity)) {
                            switch (position) {
                                //帐号管理
                                case 0:
                                    startActivity(new Intent(MainActivity.mActivity, ZhangHuActivity.class));
                                    break;
                                //孕期信息
                                case 1:
                                    startActivity(new Intent(MainActivity.mActivity, YunQiXuanZeActivity.class).putExtra("PAGE_INTENT", "MineFragment"));
                                    break;
                                //就诊医院
//                                case 2:
//                                    startActivity(new Intent(MainActivity.mActivity, JiuZhenActivity.class).putExtra("SIGN", "return"));
//                                    break;
                                //我的预约
                                case 2:
                                    startActivity(new Intent(MainActivity.mActivity, MyOrderActivity.class));
                                    break;
                                //我的订单
                                case 3:
                                    startActivity(new Intent(MainActivity.mActivity, MineOrderActivity.class));
                                    break;
                                //建议反馈
                                case 4:
                                    startActivity(new Intent(MainActivity.mActivity, FeedBackActivity.class));
                                    break;
                                //健康档案
                                case 5:
                                    //判断用户是否为VIP
//                                    if (db.select("isVIP") != null && db.select("isVIP").equals("1")) {
//                                        //判断用户是否填写过身份信息
                                        if (db.select("identityId") != null && !db.select("identityId").equals("") && !db.select("identityId").equals("null")) {
                                            startActivity(new Intent(MainActivity.mActivity, HealthRecordsActivity.class));
                                        } else {
                                            startActivity(new Intent(MainActivity.mActivity, PersonalActivity.class).putExtra("type", "1"));
                                        }
//                                    } else {
//                                        startActivity(new Intent(MainActivity.mActivity, VIPActivity.class));
//                                    }
                        break;
                    }
                } else {
                    ToastUtil.showToast(MainActivity.mActivity, R.string.no_network_hint);
                }
            } else {
                        //如果没有登陆，调整到登陆页面
                        startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                    }
                }
            }
        });
        //添加功能选择功能
        other_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (other_list.size() > 0 && position < 2) {
                    if (NetworkUtil.isNetworkAvailable(MainActivity.mActivity)) {
                        switch (position) {
                            //售后服务
                            case 0:
                                startActivity(new Intent(MainActivity.mActivity, ShouHouActivity.class));
                                break;
                            //购买设备
                            case 1:
                                Intent intent =new Intent();
                                intent.putExtra("title", "营养中心");
                                intent.putExtra("url", APIMethod.WeiDian);
                                intent.setClass(MainActivity.mActivity, WebActivity.class);
                                startActivity(intent);
//                                startActivity(new Intent(MainActivity.mActivity, GouMaiActivity.class));
                                break;
                        }
                    } else {
                        ToastUtil.showToast(MainActivity.mActivity, R.string.no_network_hint);
                    }
                }
            }
        });
    }

    /**
     * 功能填充
     */
    private void setFunction() {
        my_list = new ArrayList<>();
        other_list = new ArrayList<>();
        //添加个人类功能
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "账户管理");
        map.put("number", "0");
        map.put("image", R.mipmap.my_1 + "");
        my_list.add(map);
        map = new HashMap<>();
        map.put("name", "孕期信息");
        map.put("number", "0");
        map.put("image", R.mipmap.my_2 + "");
        my_list.add(map);
//        map = new HashMap<>();
//        map.put("name", "就诊医院");
//        map.put("number", "0");
//        map.put("image", R.mipmap.my_3 + "");
//        my_list.add(map);
        map = new HashMap<>();
        map.put("name", "我的预约");
        map.put("number", "0");
        map.put("image", R.mipmap.my_7 + "");
        my_list.add(map);
        map = new HashMap<>();
        map.put("name", "我的订单");
        map.put("number", "0");
        map.put("image", R.mipmap.my_8 + "");
        my_list.add(map);
        map = new HashMap<>();
        map.put("name", "建议反馈");
        map.put("number", "0");
        map.put("image", R.mipmap.fankui + "");
        my_list.add(map);
        map = new HashMap<>();
        map.put("name", "健康档案");//健康档案
        map.put("number", "0");
        map.put("image", R.mipmap.my_9+"");//R.mipmap.my_9
        my_list.add(map);
        map = new HashMap<>();
        map.put("name", "");
        map.put("number", "0");
        map.put("image", "");
        my_list.add(map);
        map = new HashMap<>();
        map.put("name", "");
        map.put("number", "0");
        map.put("image", "");
        my_list.add(map);
        my_gridview.setAdapter(new MyFunctionAdapter(getActivity(), my_list));
        //添加其他类功能
        map = new HashMap<>();
        map.put("name", "售后服务");
        map.put("number", "0");
        map.put("image", R.mipmap.my_5 + "");
        other_list.add(map);
        map = new HashMap<>();
        map.put("name", "购买设备");
        map.put("number", "0");
        map.put("image", R.mipmap.my_4 + "");
        other_list.add(map);
        map = new HashMap<>();
        map.put("name", "");
        map.put("number", "0");
        map.put("image", "");
        other_list.add(map);
        map = new HashMap<>();
        map.put("name", "");
        map.put("number", "0");
        map.put("image", "");
        other_list.add(map);
        other_gridview.setAdapter(new MyFunctionAdapter(getActivity(), other_list));
    }

    public void getData() {
        setMine();
    }

    private void setMine() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Request request = new Request.Builder().url(APIMethod.UserInfo).addHeader("Authorization", db.select("token_type") + " " + db.select("access_token")).get().build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();
                    int code = response.code();
                    String bodys = response.body().string();
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        msg2.obj = bodys;
                        handler1.sendMessage(msg2);

                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.arg1 = code;
                        msg1.obj = bodys;
                        handler1.sendMessage(msg1);
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == FORRESULT) {
            String result = data.getExtras().getString("HOSPTIALNAME");
            int id = data.getExtras().getInt("HOSPTIALID");
            ToastUtil.showToast(MainActivity.mActivity, result + "、、、、" + id);
            if (!TextUtils.isEmpty(result)) {
                jiuzhenyiyuan(id);
            }
        }
    }


    private void jiuzhenyiyuan(final int id) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    HashMap<String, Integer> param = new HashMap<>();
                    param.put("antenatalCareHospitalId", id);
                    String p = new Gson().toJson(param);
                    RequestBody formBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), p);
                    Request request = new Request.Builder().url(APIMethod.JiuZhenHospital_Sumit).addHeader("Authorization", db.select("token_type") + " " + db.select("access_token")).put(formBody).build();
                    okhttp3.Response response = MuYingApplication.mOkHttpClient.newCall(request).execute();
                    int code = response.code();
                    String bodys = response.body().string();
                    if (code == 204 || code == 200) {
                        Message msg2 = new Message();
                        msg2.what = 0;
                        msg2.obj = "";
                        handler1.sendMessage(msg2);
                    } else {
                        Message msg1 = new Message();
                        msg1.what = 1;
                        msg1.arg1 = code;
                        msg1.obj = "";
                        handler1.sendMessage(msg1);
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                //下载成功
                case 0:
                    if (!msg.obj.toString().equals("")) {
                        onJsonMine(msg.obj.toString());
                    }
                    break;
                //下载失败
                case 1:
//                    ToastUtil.showToast(getActivity(), "修改失败");
                    break;
            }
        }
    };

    /**
     * 显示基本信息
     *
     * @param bodys
     */
    private void onJsonMine(String bodys) {
        try {
            //设置头像
            String headPhoto = Constants.getStringByName(getActivity(), "headPhoto");
            int number = 0;
            if (headPhoto != null && !headPhoto.equals("") && !headPhoto.equals("null")) {
                number = Integer.parseInt(headPhoto);
            }
            image.setImageResource(Integer.parseInt(list.get(number).get("img")));
            //设置数据
            JSONObject jsonObject = new JSONObject(bodys);
            String pregnancyStatus = jsonObject.optString("pregnancyStatus");
            String antenatalCareHospitalName = jsonObject.optString("antenatalCareHospitalName");
            String vipEndTime = jsonObject.optString("vipEndTime");
            user_linearlayout.setVisibility(LinearLayout.VISIBLE);
            if (vipEndTime != null && !vipEndTime.equals("") && !vipEndTime.equals("null")) {
                String[] strs = vipEndTime.split("T");
                vip_linearlayout.setVisibility(LinearLayout.VISIBLE);
                vip_imageview.setImageResource(R.mipmap.yes_vip);
                vip_endtime.setText("会员有效期至：" + strs[0]);
                if (DateUtils.getStringToDate2(strs[0]) - oneDay * 30 <= DateUtils.getNumberDate1()) {
                    if (xufei.getVisibility() == View.GONE) {
                        xufei.setVisibility(Button.VISIBLE);
                    }
                } else {
                    if (xufei.getVisibility() == View.VISIBLE) {
                        xufei.setVisibility(Button.GONE);
                    }
                }
            } else {
                vip_imageview.setImageResource(R.mipmap.no_vip);
                vip_linearlayout.setVisibility(LinearLayout.GONE);
            }
            if (pregnancyStatus.equals("0")) {
                yunqi_textview.setText("备孕期");
                yunqi_textview.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.huaiyun_1, 0, 0, 0);
            } else if (pregnancyStatus.equals("1")) {
                yunqi_textview.setText("怀孕期");
                yunqi_textview.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.beiyun_1, 0, 0, 0);
            } else if (!pregnancyStatus.equals("") && !pregnancyStatus.equals("null")) {
                yunqi_textview.setText("宝宝已出生");
                yunqi_textview.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.baby_1, 0, 0, 0);
            } else {
                yunqi_textview.setText("请选择孕期");
                yunqi_textview.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
//        getActivity().unregisterReceiver(receiver);
        super.onDestroy();
    }

    View.OnClickListener onclicklistener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            switch (v.getId()) {
                //头像
                case R.id.image:
                    if (userName != null && !userName.equals("") && !userName.equals("null")) {
                        try {
                            ZhugeSDK.getInstance().track(getActivity().getApplicationContext(), "头像选择");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        choiceHeadPortrait();
                    } else {
                        //如果没有登陆，调整到登陆页面
                        startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                    }
                    break;
                //登陆页面
                case R.id.deng_lu:
                    startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                    break;
                //注册按钮
                case R.id.zhu_ce:
                    Intent zhu_ceintent = new Intent(MainActivity.mActivity, YanZhengActivity.class);
                    zhu_ceintent.putExtra("tag", "0");
                    startActivity(zhu_ceintent);
                    break;
                //会员续费
                case R.id.xufei:
                    startActivity(new Intent(MainActivity.mActivity, VIPActivity.class));
                    break;
                //设置
                case R.id.shezhi:
                    if (userName != null && !userName.equals("") && !userName.equals("null")) {
                        startActivity(new Intent(MainActivity.mActivity, PersonalActivity.class).putExtra("type", "0"));
                    } else {
                        //如果没有登陆，调整到登陆页面
                        startActivity(new Intent(MainActivity.mActivity, DengLuActivity.class));
                    }
                    break;
            }
        }
    };
    //加载框
    MyAlertDialog dialog;

    /**
     * 头像选择
     */
    private void choiceHeadPortrait() {
        try {
            //加载弹出框
            dialog = new MyAlertDialog();
            dialog.Head_Dialog(getContext(), R.layout.head_dialog);
            //获取控件
            GridView gridview = dialog.getGridView();
            //设置适配器
            String headPhoto = Constants.getStringByName(getActivity(), "headPhoto");
            int number = 0;
            if (headPhoto != null && !headPhoto.equals("") && !headPhoto.equals("null")) {
                number = Integer.parseInt(headPhoto);
            }
            ChoiceHeadPortraitAdapter adapter = new ChoiceHeadPortraitAdapter(getContext(), list, number);
            gridview.setAdapter(adapter);
            //设置点击事件
            gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Constants.setStringByName(getActivity(), "headPhoto", i + "");
                    dialog.dismiss();
                    image.setImageResource(Integer.parseInt(list.get(i).get("img")));
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //设置头像
    private void setHead() {
        list = new ArrayList<>();
        HashMap<String, String> map = new HashMap<>();
        map.put("name", "默认");
        map.put("img", R.mipmap.moren + "");
        list.add(map);
        map = new HashMap<>();
        map.put("name", "女汉子");
        map.put("img", R.mipmap.nvhanzi1 + "");
        list.add(map);
        map = new HashMap<>();
        map.put("name", "萌妹子");
        map.put("img", R.mipmap.mengmeizi1 + "");
        list.add(map);
        map = new HashMap<>();
        map.put("name", "屌丝男");
        map.put("img", R.mipmap.diaosinan1 + "");
        list.add(map);
        map = new HashMap<>();
        map.put("name", "小王子");
        map.put("img", R.mipmap.xiaowangzi1 + "");
        list.add(map);
        map = new HashMap<>();
        map.put("name", "小公主");
        map.put("img", R.mipmap.xiaogongzhu1 + "");
        list.add(map);
        map = new HashMap<>();
        map.put("name", "型男");
        map.put("img", R.mipmap.xingnan1 + "");
        list.add(map);
    }

    /**
     * 分类列表适配器
     */
    class ChoiceHeadPortraitAdapter extends BaseAdapter {
        LayoutInflater lf = null;
        ArrayList<HashMap<String, String>> getData = new ArrayList<HashMap<String, String>>();
        ViewHolder viewHolder = null;
        Context context = null;
        int number = 0;

        public ChoiceHeadPortraitAdapter(Context context,
                                         ArrayList<HashMap<String, String>> getData, int number) {
            lf = LayoutInflater.from(context);
            this.context = context;
            this.getData = getData;
            this.number = number;
        }

        @Override
        public int getCount() {
            return getData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                viewHolder = new ViewHolder();
                convertView = lf.inflate(R.layout.head_item, null);
                viewHolder.name = (TextView) convertView.findViewById(R.id.name);
                viewHolder.img = (ImageView) convertView.findViewById(R.id.img);
                viewHolder.img_ok = (ImageView) convertView.findViewById(R.id.img_ok);
                convertView.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }
            viewHolder.img.setImageResource(Integer.parseInt(getData.get(position).get("img")));
            viewHolder.name.setText(getData.get(position).get("name"));
            if (number == position) {
                if (viewHolder.img_ok.getVisibility() == View.GONE) {
                    viewHolder.img_ok.setVisibility(ImageView.VISIBLE);
                }
            } else {
                if (viewHolder.img_ok.getVisibility() == View.VISIBLE) {
                    viewHolder.img_ok.setVisibility(ImageView.GONE);
                }
            }
            return convertView;
        }
    }

    /**
     * 包装类
     */
    private static class ViewHolder {
        private TextView name = null;
        private ImageView img = null;
        private ImageView img_ok = null;
    }

    /**
     * 显示缓存数据
     */
    private void cache() {
        yong_hu.setVisibility(View.VISIBLE);
        no_user.setVisibility(View.GONE);
        user_linearlayout.setVisibility(LinearLayout.VISIBLE);
        //设置头像
        String headPhoto = Constants.getStringByName(getActivity(), "headPhoto");
        int number = 0;
        if (headPhoto != null && !headPhoto.equals("") && !headPhoto.equals("null")) {
            number = Integer.parseInt(headPhoto);
        }
        image.setImageResource(Integer.parseInt(list.get(number).get("img")));
        //设置用户
        yong_hu.setText(userName.replaceAll("(\\d{3})\\d{5}(\\d{3})", "$1*****$2"));
        if (db.select("vipEndTime") != null && !db.select("vipEndTime").equals("") && !db.select("vipEndTime").equals("null")) {
            yunqi_textview.setVisibility(TextView.VISIBLE);
            vip_linearlayout.setVisibility(LinearLayout.VISIBLE);
            vip_imageview.setImageResource(R.mipmap.yes_vip);
            String[] strs = db.select("vipEndTime").split("T");
            vip_endtime.setText("会员有效期至：" + strs[0]);
        }
        //设置孕期
        String pregnancyStatus = db.select("pregnancyStatus");
        if (pregnancyStatus != null) {
            if (pregnancyStatus.equals("0")) {
                yunqi_textview.setText("备孕期");
                yunqi_textview.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.huaiyun_1, 0, 0, 0);
            } else if (pregnancyStatus.equals("1")) {
                yunqi_textview.setText("怀孕期");
                yunqi_textview.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.beiyun_1, 0, 0, 0);
            } else if (!pregnancyStatus.equals("") && !pregnancyStatus.equals("null")) {
                yunqi_textview.setText("宝宝已出生");
                yunqi_textview.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.baby_1, 0, 0, 0);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
    }
}


